package server;
import msgs.*;

/**
 * Generic class to do things in a loop. Right now just trying to keep our db updated of clients who may have just died.
 * Team 9: Andrew Hayworth, Brian Parrella, Ryan Kortmann, Nina Papa
 * @author Andrew Hayworth
 */

public class Looper extends Thread implements TTConstants {
	private boolean go = true;
	
	public void run() {
		while (go) {
			// first get the names of everyone.
			String[] users = TTServer.connTable.keySet().toArray(new String[0]);
			for ( String s : users) {
				if ( (System.currentTimeMillis() - TTServer.lastTalkTime.get(s)) > 120000 ) {
					// they're dead to us.
					TTServer.connTable.remove(s);
					System.out.println(" removed : " + s);
				}
				else if ( (System.currentTimeMillis() - TTServer.lastTalkTime.get(s)) > 90000) {
					ClientSender.outQ.add(new ClientMsg(TTServer.connTable.get(s), Actions.KEEP_ALIVE));
				}
			}
			
			
			try {
				Thread.sleep(30000);
			}
			catch (InterruptedException e) { e.printStackTrace(); }
		}
	}
	
	public void killLooper() {
		
		go = false;
	}
}
